﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Jc.MicroDesigner.Objects;
using Jc.MicroDesigner.Management;

namespace Jc.MicroDesigner.Drawings
{
    /// <summary>
    /// Простой редактор сигналов
    /// </summary>
    public partial class PatternsEditor : UserControl
    {
        private ObjectManager _Manager = null;
        private Point ActiveView = new Point(0, 0);
        private int column_view_count = 24;
        private int row_view_count = 8;

        private List<ISignalGenerator> _Generators = new List<ISignalGenerator>();

        bool locked_post = false;

        /// <summary>
        /// Создать форму редактора
        /// </summary>
        public PatternsEditor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Показать редактор для текущей схемы
        /// </summary>
        /// <param name="manager">Логическая схема</param>
        public void Show(ObjectManager manager)
        {
            _Manager = manager;
            for (int i = 0; i < _Manager.Generators.Count; i++)
            {
                if (_Manager.Generators[i].Editable)
                {
                    _Generators.Add(_Manager.Generators[i]);
                    if (_Manager.Generators[i].SignalsCollection.Capacity == 0)
                        _Manager.Generators[i].SignalsCollection.Capacity = ObjectManager.MaxTactCount;
                }
            }
            ChangeView(0, 0);
        }

        /// <summary>
        /// Установить область просмотра
        /// </summary>
        /// <param name="xpos">X</param>
        /// <param name="ypos">Y</param>
        public void ChangeView(int xpos, int ypos)
        {
            ActiveView = new Point(xpos, ypos);

            SignalsEditor.Columns.Clear();
            SignalsEditor.Rows.Clear();
            SignalsEditor.Columns.Add(LabelColumn);
            for (int i = xpos; (i < column_view_count + xpos) && (i < ObjectManager.MaxTactCount); i++)
            {
                DataGridViewColumn col = new DataGridViewTextBoxColumn();
                col.ValueType = typeof(sbyte);
                col.Width = 40;
                col.HeaderText = (i + 1).ToString();
                SignalsEditor.Columns.Add(col);
            }
            locked_post = true;
            for (int i = ypos; (i < ypos + row_view_count) && (i < _Generators.Count); i++)
            {
                int row = SignalsEditor.Rows.Add();
                SignalsEditor[0, row].Value = _Generators[i].SignalsCollection.Source.Name;
                for (int j = xpos; (j < column_view_count + xpos) && (j < ObjectManager.MaxTactCount); j++)
                {
                    SignalsEditor[j - xpos + 1, row].Value = _Generators[i].SignalsCollection[j];
                }
            }
            locked_post = false;
        }

        void SignalsEditor_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((_Manager != null) && (!locked_post))
                if ((e.RowIndex + ActiveView.Y < _Generators.Count) && (e.ColumnIndex + ActiveView.X < ObjectManager.MaxTactCount))
                {
                    if ((SignalsEditor.Columns[e.ColumnIndex].ValueType == typeof(SByte)) && 
                        (SignalsEditor[e.ColumnIndex, e.RowIndex].Value != null))
                        _Generators[e.RowIndex + ActiveView.Y].SignalsCollection[e.ColumnIndex + ActiveView.X - 1] =
                            Convert.ToSByte(SignalsEditor[e.ColumnIndex, e.RowIndex].Value);
                }
        }
    }
}